local app = require 'app'
local game = app.game.running

local resumer = game.ui.element()

resumer.store_watchers = 
{
    ingame = 
    {
        ['$'] = function()

        end,

        ['恢复游戏场景'] = function(self, store, state)
            self:run(state.scene)
        end
    }
}

function resumer:ctor(ingame)
    self.res = 'club.threecard.doudizhu/ui/ingame.b:resumer'
    self.ingame = ingame
end

function resumer:run(scene)
    self.ingame:clear_scene()

    state = game.store.ingame.state
    if state.phase_id == game.util.consts.phase_nil.id or
        state.phase_id == game.util.consts.phase_idle.id then
            if game.kbe.player().is_ready == 1 then
                self.ingame.ready:hide()
            else
                self.ingame.ready:show()
            end
            for i = 1, self.ingame.seats.max do
                local seat = self.ingame.seats[i]
                if seat and seat.player then
                    seat:ready_change(seat.player.is_ready == 1)
                end
            end
            return
    end

    self.ingame.seen_card.poker_container:mopai(state.seen_card_ids)

    if state.phase_id == game.util.consts.phase_bid.id then
        local current_player = game.store.room.state.playing_players[scene.current_no]
        self.ingame.bidder:on_bid(current_player, scene.action_ids, scene.left_time)

        for _, public_bid_ids in pairs(scene.public_bid_ids) do
            local player = game.store.room.state.playing_players[public_bid_ids.no]
            local seat = self.ingame.seats[player.seat.local_no]
            seat.bid_result:on_result(public_bid_ids.action_id)
        end
    end

    local find_handout = function(no)
        for _, public_card_ids in ipairs(scene.public_card_ids) do
            if public_card_ids.no == no then
                return public_card_ids
            end
        end

        return nil
    end

    for local_no, card_ids in pairs(state.hand_card_ids) do
        local seat = self.ingame.seats[local_no]
        local public_card_ids = find_handout(seat.player.seat.no)
        if public_card_ids then
            seat.poker_container:fill(card_ids, public_card_ids.card_ids)
            if public_card_ids == 0 then
                seat:mark_pass(true)
            else
                seat:mark_pass(false)
            end
        else
            seat.poker_container:fill(card_ids, {})
            seat:mark_pass(false)
        end
    end

    if state.phase_id == game.util.consts.phase_handout.id then
        local current_player = game.store.room.state.playing_players[scene.current_no]
        self.ingame.seats:on_handout(current_player, scene.left_time, scene.round_first)
        if current_player == game.kbe.player() then
            self.ingame.handouter:on_handout(scene.action_ids, scene.left_time)
        end
    end

    if scene.lord_no ~= -1 then
        local lord_player = game.store.room.state.playing_players[scene.lord_no]
        self.ingame.seats:on_notice_lord(lord_player)
    end
end

return resumer